/**
 * @Author: SevDaisy十七散人 
 * @Date: 2020-07-14 21:20:18
 */
package zyy.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import zyy.util.BusinessException;
import zyy.util.Bus;

public class FrmNewProduct extends JDialog implements ActionListener {
  private static final long serialVersionUID = 658741659025403864L;
  private JPanel text = new JPanel();
  private JPanel btns = new JPanel();
  private JLabel pNameLabel = new JLabel("商品名字");
  private JTextField pNameEdt = new JTextField(20);
  private JLabel pValueLabel = new JLabel("商品原价");
  private JTextField pValueEdt = new JTextField(20);
  private JLabel pRealValueLabel = new JLabel("商品实际售价");
  private JTextField pRealValueEdt = new JTextField(20);
  private JLabel storeLabel = new JLabel("库存");
  private JTextField storeEdt = new JTextField(20);
  private JButton commit = new JButton("提交");
  private JButton exit = new JButton("退出");

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == this.commit) {
      try {
        String pName = this.pNameEdt.getText();
        double pValue = Double.parseDouble(this.pValueEdt.getText());
        double pRealValue = Double.parseDouble(this.pRealValueEdt.getText());
        double store = Double.parseDouble(this.storeEdt.getText());
        if (this.pNameEdt.getText() == null || this.pValueEdt.getText() == null || this.pRealValueEdt.getText() == null
            || this.storeEdt.getText() == null) {
          throw new BusinessException("请确保填写所有字段");
        } else {
          FrmAdminMerchant fam = Bus.fAdminMerchant;
          Bus.ProductManager.add(fam.getCurrentMerchant(), fam.getCurrentProduct_Dir(), pName, pValue, pRealValue,
              store);
          Bus.fAdminMerchant.reloadProduct_DirTable();
          Bus.fAdminMerchant.reloadProductTable();
          this.setVisible(false);
        }
      } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
      }

      this.setVisible(false);
    } else if (e.getSource() == this.exit) {
      this.setVisible(false);
    }
  }

  public FrmNewProduct() {
    // 基础 JDialog 设置
    {
      this.setTitle("外卖助手 管理员 新建商品");
      this.setSize(275, 310);
      this.setLocationRelativeTo(null);
      this.setVisible(true);
    }
    // this.getContentPane() 一级布局
    {
      this.getContentPane().setLayout(new BorderLayout());
      this.getContentPane().add(this.text, BorderLayout.CENTER);
      this.getContentPane().add(this.btns, BorderLayout.SOUTH);
    }
    // text 相关配置
    {
      this.text.setLayout(new FlowLayout(FlowLayout.LEFT));
      this.text.add(this.pNameLabel);
      this.text.add(this.pNameEdt);
      this.text.add(this.pValueLabel);
      this.text.add(this.pValueEdt);
      this.text.add(this.pRealValueLabel);
      this.text.add(this.pRealValueEdt);
      this.text.add(this.storeLabel);
      this.text.add(this.storeEdt);
    }
    // btns 相关配置
    {
      this.btns.setLayout(new FlowLayout(FlowLayout.RIGHT));
      this.btns.add(this.commit);
      this.btns.add(this.exit);
      this.commit.addActionListener(this);
      this.exit.addActionListener(this);
      Bus.jButtonZYYStyle(this.commit);
      Bus.jButtonZYYStyle(this.exit);
    }
  }

}